/*!
 * \file frdb_frlist_copy.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include <frdb_cursor.h>

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __test_node_free(Frlist_node *p_st_node)
{
    if(p_st_node == NULL || p_st_node->data == NULL)
        return;

    Frdata_field *p_st_data = (Frdata_field *)FRLIST_GET_NODE_DATA(p_st_node);

    if(p_st_data->p_field_name)
        frfree(p_st_data->p_field_name);

    if(p_st_data->p_field_value)
        frfree(p_st_data->p_field_value);

    frfree(p_st_data);

    return;
}

static void __test_node_show(Frlist_node *p_st_node, char *p_outbuff, unsigned int ui_buff_size)
{
    if(p_st_node == NULL || p_st_node->data == NULL)
        return;

    Frdata_field *p_st_data = (Frdata_field *)FRLIST_GET_NODE_DATA(p_st_node);

    snprintf(p_outbuff, 1024, "%s = %s\n", p_st_data->p_field_name, p_st_data->p_field_value);

    return;
}

static Frdata_field* frdata_field_str(const char *p_name, const char *p_value)
{
    Frdata_field *p_st_data = NULL;

    p_st_data = (Frdata_field *)frmalloc(sizeof(Frdata_field));
    memset(p_st_data, 0x00, sizeof(Frdata_field));

    p_st_data->p_field_name = frstrdup(p_name);
    p_st_data->p_field_value = frstrdup(p_value);

    return p_st_data;
}

static void __case1(void)
{
    Frlist_header *p_lst_src = NULL;
    Frlist_header *p_lst_dest = NULL;
    Frlist_node *p_st_node = NULL;
    Frdata_field *p_st_data = NULL;
    int i_ret = 0;
    char *p_out = NULL;

    p_lst_src = frlist_create();

    p_st_data = frdata_field_str((char *)"name1", (char *)"value1");
    p_st_node = frlist_node_create(p_st_data);
    frlist_push(p_lst_src, p_st_node);

    p_st_data = frdata_field_str((char *)"name2", (char *)"value2");
    p_st_node = frlist_node_create(p_st_data);
    frlist_push(p_lst_src, p_st_node);

    p_st_data = frdata_field_str((char *)"name3", (char *)"value3");
    p_st_node = frlist_node_create(p_st_data);
    frlist_push(p_lst_src, p_st_node);

    p_lst_dest = frlist_create();

    i_ret = frdb_frlist_copy(p_lst_src, p_lst_dest);

    p_out = (char *)frmalloc(1024);

    frlist_show(p_lst_dest, p_out, 1024, __test_node_show);
    printf("%s", p_out);
    frfree(p_out);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frlist_destroy(p_lst_src, __test_node_free);
    frlist_destroy(p_lst_dest, __test_node_free);

    return;

}

static void __case2(void)
{
    Frlist_header *p_lst_src = NULL;
    Frlist_header *p_lst_dest = NULL;
    int i_ret = 0;
    char *p_out = NULL;

    p_lst_src = frlist_create();


    p_lst_dest = frlist_create();

    i_ret = frdb_frlist_copy(p_lst_src, p_lst_dest);

    p_out = (char *)frmalloc(1024);
    memset(p_out, 0x00, 1024);

    frlist_show(p_lst_dest, p_out, 1024, __test_node_show);
    printf("%s", p_out);
    frfree(p_out);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    frlist_destroy(p_lst_src, __test_node_free);
    frlist_destroy(p_lst_dest, __test_node_free);

    return;

}


int add_suite_frdb_frlist_copy(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frdb_cursor frdb_frlist_copy()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);


    return FR_SUCC;
}
